package org.zfes.snowier.common.monitor.biz.service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.zfes.snowier.common.monitor.biz.dao.GcMapper;
import org.zfes.snowier.common.monitor.biz.model.GcEntity;
import org.zfes.snowier.common.monitor.util.ByteKbUtils;
import org.zfes.snowier.common.monitor.util.JvmUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;


@Service
public class GcService {
    @Autowired
    private GcMapper gcMapper;

    @SuppressWarnings({ "unchecked", "rawtypes" })
	public List<GcEntity> findAllByInstanceKey(String instanceKey) {
    	Map<String, Object> paramMap=Maps.newHashMap();
    	paramMap.put("instanceKey", instanceKey);
        return gcMapper.selectList(new QueryWrapper(paramMap));
    }
    public void save( GcEntity entity) {
    	entity.setDate(JvmUtils.time());
    	gcMapper.insert(entity);
    }

    @Async
    public void save(String instanceKey,String gcjsonData) {
    	 GcEntity gcz =	JSONObject.parseObject(gcjsonData, GcEntity.class);
    	 gcz.setS0C(ByteKbUtils.getMB(gcz.getS0C()));
    	 gcz.setS1C(ByteKbUtils.getMB(gcz.getS1C()));
    	 gcz.setS0U(ByteKbUtils.getMB(gcz.getS0U()));
    	 gcz.setS1U(ByteKbUtils.getMB(gcz.getS1U()));
    	 
    	 gcz.setEC(ByteKbUtils.getMB(gcz.getEC()));
    	 gcz.setEU(ByteKbUtils.getMB(gcz.getEU()));
    	 gcz.setOC(ByteKbUtils.getMB(gcz.getOC()));
    	 gcz.setOU(ByteKbUtils.getMB(gcz.getOU()));
    	 gcz.setMC(ByteKbUtils.getMB(gcz.getMC()));
    	 gcz.setMU(ByteKbUtils.getMB(gcz.getMU()));
    	 gcz.setCCSU(ByteKbUtils.getMB(gcz.getCCSU()));
    	 gcz.setCCSC(ByteKbUtils.getMB(gcz.getCCSC()));
    	 
    	 gcz.setInstanceKey(instanceKey);
    	 gcz.setGroupKey(StringUtils.substringBefore(instanceKey, "_"));
    	 save(gcz);
    }
 
    
    @Async
    public void clearAll() {
    	gcMapper.deleteAll();
    }
}
